package com.bunde.repo;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.eventusermodel.XSSFBReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

// 对于大数据量的excel文件加载需要直接通过SAX对excel底层的的XML数据进行操作
public class ExcelSAXReaderManager extends DefaultHandler {

    public Object saxLoadFile(String filename){
        Map<String, Object> mp = new HashMap<>();



        return mp;
    }

    public void process(String filename) throws Exception {
        OPCPackage opcPackage = OPCPackage.open(new File(filename));
        XSSFBReader xssfbReader = new XSSFBReader(opcPackage);
        StylesTable stylesTable = xssfbReader.getStylesTable();
        SharedStringsTable sharedStringsTable = xssfbReader.getSharedStringsTable();
        XMLReader xmlReader = XMLReaderFactory.createXMLReader("javax.xml.parsers.SAXParser");
        xmlReader.setContentHandler(this);
        XSSFBReader.SheetIterator sheetsData = (XSSFBReader.SheetIterator) xssfbReader.getSheetsData();
        while(sheetsData.hasNext()){
            InputStream sheet = sheetsData.next();
            String sheetName = sheetsData.getSheetName();
            System.out.println("sheetName = " + sheetName);
            InputSource inputSource = new InputSource(sheet);
            xmlReader.parse(inputSource);
            sheet.close();
        }
    }


    /**
     * 第一个
     * @param uri
     * @param localName
     * @param qName
     * @param attributes
     * @throws SAXException
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        System.out.println("startElement: " + uri + ", " + localName + ", " + qName + ", " + attributes);
    }

    /**
     * 第二个
     * @param ch
     * @param start
     * @param length
     * @throws SAXException
     */
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        System.out.println("characters: " + new String(ch) + ", " + start + ", " + length);
    }

    /**
     * 第三个
     * @param uri
     * @param localName
     * @param qName
     * @throws SAXException
     */
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        System.out.println("endElement: " + uri + ", " + localName + ", " + qName);
    }
}
